package com.th.supcom.pds.phamapply.client.widget.inpharmacyreturn;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Component;
import com.google.gwt.event.dom.client.KeyCodes;
import com.th.supcom.pds.common.constant.KeysConstants;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyPatInfoModel;

/**
 * 门诊退药时间监听
 * 
 * @author yzhang
 */
public class NurseReturnListener
{
    private NurseReturnFields m_nurseReturnFields;
    private NurseReturnButtons m_nurseReturnButtons;
    private NurseReturnAction m_nurseReturnAction;
    /** 需要监听的全局面板. */
    private Component m_keyNavComponent;
    private final List <Component> m_widgetList = new ArrayList <Component> ();

    /**
     * 初始化所有的Listener
     */
    public void initListeners ()
    {
        m_widgetList.add (m_nurseReturnFields.getBedNoField ());
        m_widgetList.add (m_nurseReturnFields.getPatiIdField ());
        m_widgetList.add (m_nurseReturnFields.getWatchTimes ());
        m_widgetList.add (m_nurseReturnButtons.getReturnBill ());
        m_widgetList.add (m_nurseReturnFields.getBeginField ());
        m_widgetList.add (m_nurseReturnFields.getEndField ());
        m_widgetList.add (m_nurseReturnButtons.getQueryBtn ());
        addButtonListener ();
        addFieldListener ();
        addEnterKeyListener ();
        addFocusListeners ();
    }

    /**
     * 界面按钮监听事件
     */
    private void addButtonListener ()
    {
        m_nurseReturnButtons.getQueryBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_nurseReturnAction.findChargeItemByExample ();
            }
        });
        m_nurseReturnButtons.getComfirmBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_nurseReturnAction.confirm (m_nurseReturnFields.getBedNoField (), m_nurseReturnButtons.getComfirmBtn ());
            }
        });
        m_nurseReturnButtons.getClearBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_nurseReturnAction.clear (true);
            }
        });
    }

    public void addFieldListener ()
    {
        m_nurseReturnFields.getPatiIdField ().addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (ComponentEvent event)
            {
                if (event.getKeyCode () == KeyCodes.KEY_ENTER)
                {
                    m_nurseReturnAction.findInPatientInfoByPatientId (m_nurseReturnFields.getPatiIdField ().getValue ());
                }
            }
        });
        m_nurseReturnFields.getBedNoField ().addKeyListener (new KeyListener ()
        {
            @Override
            public void componentKeyDown (ComponentEvent event)
            {
                if (event.getKeyCode () == KeyCodes.KEY_ENTER)
                {
                    String bedNo = m_nurseReturnFields.getBedNoField ().getValue ();
                    m_nurseReturnAction.getPatiInfoByBedNo (bedNo);
                }
            }
        });
        m_nurseReturnFields.getWatchTimes ()
                           .addSelectionChangedListener (new SelectionChangedListener <InpPhamApplyPatInfoModel> ()
                           {
                               @Override
                               public void selectionChanged (SelectionChangedEvent <InpPhamApplyPatInfoModel> se)
                               {
                                   if (se != null)
                                   {
                                       m_nurseReturnAction.watchTimesChange ();
                                   }

                               }
                           });
        // m_nurseReturnFields.getPatiAdtComboBox ()
        // .addSelectionChangedListener (new SelectionChangedListener
        // <PtsPaiAdtLogModel> ()
        // {
        // @Override
        // public void selectionChanged (SelectionChangedEvent
        // <PtsPaiAdtLogModel> se)
        // {
        // if (se != null)
        // {
        // m_nurseReturnAction.patiAdtChange ();
        // }
        // }
        // });
    }

    /**
     * 监听快捷键
     */
    private void addEnterKeyListener ()
    {
        new KeyNav <ComponentEvent> (m_keyNavComponent)
        {
            @Override
            public void onKeyPress (ComponentEvent ce)
            {
                int keyCode = ce.getKeyCode ();
                if (ce.isAltKey ())
                {
                    executAltEvent (keyCode);
                }

            }
        };

    }

    /**
     * 执行Alt+?快捷键
     */
    private void executAltEvent (int keyCode)
    {
        if (keyCode == KeysConstants.VK_Q)
        {
            m_nurseReturnAction.findChargeItemByExample ();
        }
        if (keyCode == KeysConstants.VK_O)
        {
            m_nurseReturnAction.confirm (m_nurseReturnFields.getBedNoField (), m_nurseReturnButtons.getComfirmBtn ());
        }
        if (keyCode == KeysConstants.VK_R)
        {
            m_nurseReturnAction.clear (true);
        }
    }

    /**
     * 监听焦点事件
     */
    private void addFocusListeners ()
    {

        ClientUtils.getFocusChange (null, m_nurseReturnButtons.getComfirmBtn (), m_nurseReturnButtons.getQueryBtn ());
        ClientUtils.getFocusChange (m_nurseReturnButtons.getQueryBtn (), m_nurseReturnButtons.getClearBtn (),
                                    m_nurseReturnButtons.getComfirmBtn ());
        ClientUtils.getFocusChange (m_nurseReturnButtons.getComfirmBtn (), null, m_nurseReturnButtons.getClearBtn ());
    }

    // // private void shortCutListener ()
    // // {
    // // for (int i = 0; i < m_widgetList.size () - 1; i++)
    // // {
    // // final int index = i;
    // // m_widgetList.get (i).addListener (Events.KeyUp, new Listener
    // <ComponentEvent> ()
    // // {
    // // @Override
    // // public void handleEvent (ComponentEvent event)
    // // {
    // // if (KeysConstants.VK_ENTER == event.getKeyCode ())
    // // {
    // // if (index == 1 && !"".equals (m_nurseReturnFields.getPatiIdField
    // ().getValue ()))
    // // {
    // // m_nurseReturnAction.findInPatientInfoByPatientId
    // (m_nurseReturnFields.getPatiIdField ().getValue ());
    // // }
    // // m_widgetList.get (index + 1).focus ();
    // // return;
    // // }
    // // }
    // // });
    // // }
    // }

    public void setKeyNavComponent (Component keyNavComponent)
    {
        m_keyNavComponent = keyNavComponent;
    }

    public void setNurseReturnFields (NurseReturnFields nurseReturnFields)
    {
        m_nurseReturnFields = nurseReturnFields;
    }

    public void setNurseReturnButtons (NurseReturnButtons nurseReturnButtons)
    {
        m_nurseReturnButtons = nurseReturnButtons;
    }

    public void setNurseReturnAction (NurseReturnAction nurseReturnAction)
    {
        m_nurseReturnAction = nurseReturnAction;
    }

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
